<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" layout:decorate="~{main}"
      xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout">
<link th:href="@{/localcss/crmlist.css}" href="..atic/localcss/crmadd.css" rel="stylesheet">
<head>
    <title>角色编辑</title>
</head>
<body>
<div layout:fragment="content">
    <div class="">
        <div class="clearfix"></div>
        <div class="row">
            <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                    <div class="x_title">
                        <h2>编辑角色信息 <i class="fa fa-user"></i></h2>
                        <div class="clearfix"></div>
                    </div>
                    <div class="x_content">
                        <div class="clearfix"></div>
                        <form class="form-horizontal form-label-left" th:action="@{/role/save}" action="/role/save" method="post">
                            <input type="hidden" name="roleId" th:value="${role.roleId}" />
                            <div class="item form-group">
                                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="roleName"> 角色名称 <span class="required">*</span>
                                </label>
                                <div class="col-md-6 col-sm-6 col-xs-12">
                                    <input id="roleName" class="form-control col-md-7 col-xs-12"
                                           data-validate-length-range="10" data-validate-words="1" name="roleName" required="required"
                                           placeholder="请输入角色名称" type="text"
                                           th:value="${role.roleName}">
                                </div>
                            </div>
                            <div class="item form-group">
                                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="roleDesc"> 角色描述
                                </label>
                                <div class="col-md-6 col-sm-6 col-xs-12">
                                    <input id="roleDesc" class="form-control col-md-7 col-xs-12"
                                           data-validate-length-range="10" data-validate-words="1" name="roleDesc"
                                           placeholder="请输入描述" type="text"
                                           th:value="${role.roleDesc}" />
                                </div>
                            </div>
                            <div class="item form-group">
                                <label class="control-label col-md-3 col-sm-3 col-xs-12" for="roleFlag"> 角色状态
                                </label>
                                <div class="col-md-6 col-sm-6 col-xs-12">
                                    <input id="roleFlag" name="roleFlag" type="checkbox" value="1"
                                           th:checked="${role.roleFlag==1}" />
                                    (是否启用)
                                </div>
                            </div>
                            <div class="item form-group">
                                <label class="control-label col-md-3 col-sm-3 col-xs-12"> 指派权限
                                </label>
                                <div class="col-md-6 col-sm-6 col-xs-12" style="padding-top: 7px;">
                                    <ul style="list-style: none;padding-left: 0px;">
                                        <li th:each="right1:${rights}" th:if="${right1.rightType=='Folder'}">
                                            <input type="checkbox" name="rightCodes" th:value="${right1.rightCode}"
                                                   th:checked="${role.rights.contains(right1)}" />
                                            <span th:text="${right1.rightText}">一级权限</span>
                                            <ul style="list-style: none;">
                                                <li th:each="right2:${rights}" th:if="${right2.rightType=='Document' && right2.rightParentCode==right1.rightCode}">
                                                    <input type="checkbox" name="rightCodes" th:value="${right2.rightCode}"
                                                           th:checked="${role.rights.contains(right2)}" />
                                                    <span th:text="${right2.rightText}">二级权限</span>
                                                    <ul style="list-style: none;">
                                                        <li th:each="right3:${rights}" th:if="${right3.rightType=='Url' && right3.rightParentCode==right2.rightCode}">
                                                            <input type="checkbox" name="rightCodes" th:value="${right3.rightCode}"
                                                                   th:checked="${role.rights.contains(right3)}" />
                                                            <span th:text="${right3.rightText}">三级权限</span>
                                                        </li>
                                                    </ul>
                                                </li>
                                            </ul>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-6 col-md-offset-3">
                                    <button id="send" type="submit" class="btn btn-success">保存</button>
                                    <button type="button" class="btn btn-primary" id="back">返回</button>
                                    <br /><br />
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script layout:fragment="js">
    $(document).ready(function () {
        $(":checkbox[name='rightCodes']").on("change", function () {
            // 判断当前节点是否选中
            if ($(this).is(":checked")) {
                // 选中所有子节点
                $(this).siblings("ul").find(":checkbox[name='rightCodes']").prop("checked", true);
                // 自动选中上级节点
                $(this).parent("li").parent("ul").siblings(":checkbox[name='rightCodes']").prop("checked", true);
            } else {
                // 移除所有子节点选中状态
                $(this).siblings("ul").find(":checkbox[name='rightCodes']").removeAttr("checked");
                var siblings_unchecked = true; // 兄弟节点全都选中
                // 判断其他兄弟节点是否选中
                $(this).parent("li").siblings("li").children(":checkbox[name='rightCodes']").each(function () {
                    if ($(this).is(":checked")) {
                        siblings_unchecked = false;
                        return false; // return false退出循环；return true结束本次循环继续下次循环
                    }
                });
                // 没有子项选中时移除上级节点选中状态
                if (siblings_unchecked) {
                    // 自动选中上级节点
                    $(this).parent("li").parent("ul").siblings(":checkbox[name='rightCodes']").removeAttr("checked");
                }
            }
        });

        $("#back").on("click", function () {
            window.history.back();
        });
    });
</script>
</html>